{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input, Conv2D, Activation, MaxPooling2D, Dropout, Flatten, Dense\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random_seed = 10013\n",
    "data_in_shape = (7, 7, 1)\n",
    "\n",
    "layers = [\n",
    "    Conv2D(2, (3,3), padding='valid', strides=(1,1)),\n",
    "    Activation('relu'),\n",
    "    Conv2D(2, (3,3), padding='valid', strides=(1,1)),\n",
    "    Activation('relu'),\n",
    "    MaxPooling2D(pool_size=(2,2), strides=(1,1)),\n",
    "    Dropout(0.25),\n",
    "    Flatten(),\n",
    "    Dense(3),\n",
    "    Activation('relu'),\n",
    "    Dropout(0.5),\n",
    "    Dense(3),\n",
    "    Activation('softmax')\n",
    "]\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = layers[0](input_layer)\n",
    "for layer in layers[1:-1]:\n",
    "    x = layer(x)\n",
    "output_layer = layers[-1](x)\n",
    "model = Model(inputs=input_layer, outputs=output_layer)\n",
    "\n",
    "np.random.seed(random_seed)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = format_decimal(data_in.ravel().tolist())\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['pipeline_13'] = {\n",
    "    'input': {'data': data_in_formatted, 'shape': data_in_shape},\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/pipeline/13.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"pipeline_13\": {\"input\": {\"data\": [0.314304, 0.087114, -0.192081, 0.687827, -0.05382, -0.449211, 0.152559, 0.729694, 0.99547, -0.487042, -0.486432, 0.714711, -0.292248, -0.199289, -0.242229, -0.439609, -0.575396, -0.053792, 0.860916, -0.065459, 0.353475, 0.680997, 0.830208, 0.469092, -0.265621, -0.687023, -0.45321, 0.530606, -0.319466, 0.898567, -0.021989, -0.726166, 0.350933, 0.471236, 0.214698, -0.218634, -0.656787, 0.487585, -0.45693, 0.095657, 0.951011, -0.969214, -0.540415, 0.42117, 0.268511, -0.931772, -0.026435, -0.416304, -0.229962], \"shape\": [7, 7, 1]}, \"weights\": [{\"data\": [0.314304, 0.087114, -0.192081, 0.687827, -0.05382, -0.449211, 0.152559, 0.729694, 0.99547, -0.487042, -0.486432, 0.714711, -0.292248, -0.199289, -0.242229, -0.439609, -0.575396, -0.053792], \"shape\": [3, 3, 1, 2]}, {\"data\": [0.488004, 0.911709], \"shape\": [2]}, {\"data\": [0.263717, -0.720003, 0.8268, -0.92253, 0.462425, 0.244446, -0.124898, -0.811324, 0.354873, 0.801179, 0.564291, -0.355745, 0.644611, 0.859794, -0.560491, -0.788843, -0.963575, -0.638994, 0.405345, 0.176622, -0.153501, 0.998311, 0.944831, 0.536878, 0.033269, -0.435141, -0.309305, 0.602218, 0.537614, 0.254239, 0.37362, -0.505583, -0.948316, 0.939032, 0.149339, -0.197973], \"shape\": [3, 3, 2, 2]}, {\"data\": [-0.296519, 0.572188], \"shape\": [2]}, {\"data\": [-0.983721, 0.040472, -0.976147, -0.023768, -0.481133, -0.192315, 0.766864, -0.700711, -0.549223, 0.449282, 0.713811, 0.290114, -0.96995, 0.09209, -0.529109, -0.916857, 0.914016, -0.884145, 0.24725, -0.24954, -0.752434, 0.479158, -0.887148, 0.116841], \"shape\": [8, 3]}, {\"data\": [0.524698, 0.474407, 0.471901], \"shape\": [3]}, {\"data\": [-0.040091, -0.549528, 0.347624, 0.057999, -0.900791, -0.547796, -0.152768, -0.651661, -0.474991], \"shape\": [3, 3]}, {\"data\": [-0.70434, -0.602297, -0.208904], \"shape\": [3]}], \"expected\": {\"data\": [0.266763, 0.295422, 0.437815], \"shape\": [3]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
